#include "widget.h"
#include "ui_widget.h"
#include <QDir>
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_mediaplayer = new QMediaPlayer(this);
    m_pvideowidget = new QVideoWidget(ui->playlabel);
    m_mediaplayer->setVideoOutput(m_pvideowidget);

    ui->horizontalSlider->setRange(0, 100);
    m_pvideowidget->setStyleSheet("background-color: black;");
    m_pvideowidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->playlabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 为按钮设置大小策略
    ui->playButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->pauseButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->volumeSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->importButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->volumelabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    //ui->horizontalSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->volumelabel->setAlignment(Qt::AlignRight);

    // 为进度条和音量滑块设置大小策略
    ui->horizontalSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 创建布局管理器（垂直布局）
    m_layout = new QVBoxLayout(this);
    m_hlayout = new QHBoxLayout(this);

    // 设置子布局的约束为无约束
    m_layout->setSizeConstraint(QLayout::SetNoConstraint);
    m_hlayout->setSizeConstraint(QLayout::SetNoConstraint);

    m_hlayout->addWidget(ui->playButton);       // 添加播放按钮
    m_hlayout->addWidget(ui->pauseButton);      // 添加暂停按钮
    m_hlayout->addWidget(ui->importButton);     // 添加导入按钮
    m_hlayout->addWidget(ui->volumelabel);      // 添加音量滑块
    m_hlayout->addWidget(ui->volumeSlider);     // 添加音量滑块

    m_layout->addWidget(ui->playlabel);         // 添加视频控件
    m_layout->addWidget(ui->horizontalSlider);  // 添加进度条
    m_layout->addLayout(m_hlayout);
    setLayout(m_layout);

    // 设置布局
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 强制调整大小
    connect(m_mediaplayer, SIGNAL(stateChanged(QMediaPlayer::State)),
            SLOT(OnStateChange(QMediaPlayer::State)));

    // 播放器状态变化
    connect(m_mediaplayer, &QMediaPlayer::positionChanged, this, &Widget::updatePosition);
    connect(m_mediaplayer, &QMediaPlayer::durationChanged, this, &Widget::updateDuration);
    connect(ui->volumeSlider, &QSlider::valueChanged, this, &Widget::setVolume);

    // 进度条变化
    connect(ui->horizontalSlider, &QSlider::sliderMoved, this, &Widget::seekPosition);
}

Widget::~Widget()
{
    delete m_pvideowidget;
    delete m_mediaplayer;
    delete m_layout;
    delete m_hlayout;
    delete ui;
}

void Widget::OnStateChange(QMediaPlayer::State state)
{
    //qDebug() << "state change";
    bool isPlaying = (state == QMediaPlayer::PlayingState);
    ui->playButton->setEnabled(!isPlaying);
    ui->pauseButton->setEnabled(isPlaying);
}

void Widget::on_playButton_clicked()
{
    m_mediaplayer->play();
}

void Widget::on_pauseButton_clicked()
{
    m_mediaplayer->pause();
}

void Widget::updatePosition(qint64 postion)
{
    ui->horizontalSlider->setValue(postion);
}

void Widget::updateDuration(qint64 duration)
{
    ui->horizontalSlider->setRange(0, static_cast<int>(duration));
}

void Widget::seekPosition(int position)
{
    m_mediaplayer->setPosition(position);
}

// 设置音量
void Widget::setVolume(int volume)
{
    m_mediaplayer->setVolume(volume);
}

void Widget::on_importButton_clicked()
{
    QString strCurrentPath = QDir::homePath();
    QString stdDlgTitle = "请选择视频文件";
    QString strFilter = "MP4 File(*.mp4);;All File(*.*)";
    QString strAllFiles = QFileDialog::getOpenFileName(this,
                            stdDlgTitle, strCurrentPath, strFilter);
    if (strAllFiles.isEmpty()) {
        QMessageBox::information(this, "错误", "打开视频文件失败，请重新检查", QMessageBox::Yes | QMessageBox::No);
        return ;
    }
    QFileInfo fileinfos(strAllFiles);
    qDebug() << "File info" << fileinfos.fileName() << ", path" << fileinfos.filePath();

    m_mediaplayer->setMedia(QUrl::fromLocalFile(strAllFiles));
    m_mediaplayer->play();
}
